// components/HTMLTextcontent/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    textcontent: {
      type: String,
      value: '',
      observer: function (newVal, oldVal) {

        this._textcontentpropertyChange(newVal, oldVal);

      } // 属性被改变时执行的函数（可选），也可以写成在methods段中定义的方法名字符串, 如：'_propertyChange'

    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    content:'',
  },

  /**
   * 组件的方法列表
   */
  methods: {

    _textcontentpropertyChange: function (newVal, oldVal) {
      var webcontent = "";// =decodeURIComponent(newVal);
  
      webcontent =  this.escapeImg(newVal);
     // console.log(webcontent);
      this.setData({
        content:webcontent
      });
    },
    //转义方法
    escape2Html(str) {
      var arrEntities = {
        'lt': '<',
        'gt': '>',
        'nbsp': ' ',
        'amp': '&',
        'quot': '"'
      };
      return str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all, t) {
        return arrEntities[t];
      }).replace('<section', '<div');
    },

    escapeImg(str) {
      //return str.replace('<img ', '<img style="max-width:100%;height:auto;display:block;margin:10px 0;" mode="aspectFill" ').replace("data-src=","src=");
     // console.log(str.match((/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '')));
      let html = str
        .replace(/style=""/ig, '  ')
        .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
        .replace(/<p>/ig, '<p style="font-size: 15Px; line-height: 25Px;">')
        .replace(/<img([\s\w"-=\/\.:;]+)((?:(height="[^"]+")))/ig, '<img$1')
        .replace(/<img([\s\w"-=\/\.:;]+)((?:(width="[^"]+")))/ig, '<img$1')
        .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
        .replace(/<img([\s\w"-=\/\.:;]+)((?:(alt="[^"]+")))/ig, '<img$1')
        .replace(/<img [^>]*style=['"]([^'"]+)[^>]*>/ig, '')
        .replace(/data-src/ig, ' src ')
        .replace(/\<img/gi, '<img style="max-width:100%;height:auto" mode="aspectFill" ')

      return html;



    }
  }
})